package com.huidu.bitrade.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huidu.bitrade.constant.ActivityType;
import com.huidu.bitrade.entity.Activity;
import com.huidu.bitrade.entity.Coin;
import com.huidu.bitrade.entity.HdzImport;
import com.huidu.bitrade.entity.MemberImport;
import com.huidu.bitrade.service.ActivityService;
import com.huidu.bitrade.service.CoinService;
import com.huidu.bitrade.service.HdzImportService;
import com.huidu.bitrade.service.MemberImportService;
import com.huidu.bitrade.task.HdcAwardTask;
import com.huidu.bitrade.task.HdzOrderTask;
import com.huidu.bitrade.util.MessageResult;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/hdzImport")
@RequiredArgsConstructor
public class HdzImportController {

    private final HdzImportService hdzImportService;
    private final CoinService coinService;
    private final ActivityService activityService;
    private final MemberImportService memberImportService;
    private final HdcAwardTask hdcAwardTask;

    @ApiOperation(value = "导入用户")
    @GetMapping("memberImport")
    public MessageResult memberImport() throws Exception {
        List<MemberImport> list = memberImportService.list(Wrappers.<MemberImport>lambdaQuery()
                .eq(MemberImport::getStatus,0));
        if(CollectionUtils.isEmpty(list)){
            return MessageResult.success("没有未导入的数据");
        }
        //所有币种
        List<Coin> coins = coinService.findAll();
        for (MemberImport memberImport:list) {
            memberImportService.memberImport(memberImport,coins);
        }
        return MessageResult.success();
    }


    @ApiOperation(value = "导入hdz")
    @GetMapping("import")
    public MessageResult hdzImport() throws Exception {
        List<HdzImport> list = hdzImportService.list(Wrappers.<HdzImport>lambdaQuery()
                .ne(HdzImport::getStatus,1));
        if(CollectionUtils.isEmpty(list)){
            return MessageResult.success("没有未导入的数据");
        }
        //所有币种
        List<Coin> coins = coinService.findAll();
        //hdz质押活动
        List<Activity> activity = activityService.findByTypeAndStep(ActivityType.HDZ_PLEDGE,1);
        if(CollectionUtils.isEmpty(activity)){
            throw new IllegalArgumentException("hdz质押活动未启动");
        }
        for (HdzImport hdz:list) {
            hdzImportService.hdzImport(hdz,coins,activity.get(0));
        }
        return MessageResult.success();
    }

//    @RequestMapping("/test")
//    public void test(){
//        hdzOrderTask.release();
//    }

    @RequestMapping("/chargeHdcAward")
    public void test1(){
        hdcAwardTask.chargeHdcAward();
    }

    @ApiOperation(value = "导入hdz2")
    @GetMapping("import2")
    public MessageResult import2() throws Exception {
        List<HdzImport> list = hdzImportService.list(Wrappers.<HdzImport>lambdaQuery()
                .ne(HdzImport::getStatus,1));
        if(CollectionUtils.isEmpty(list)){
            return MessageResult.success("没有未导入的数据");
        }
        //hdz质押活动
        List<Activity> activity = activityService.findByTypeAndStep(ActivityType.HDZ_PLEDGE,1);
        if(CollectionUtils.isEmpty(activity)){
            throw new IllegalArgumentException("hdz质押活动未启动");
        }
        for (HdzImport hdz:list) {
            hdzImportService.hdzImport2(hdz,activity.get(0));
        }
        return MessageResult.success();
    }

}
